package com.dengcj.epl.service.ecology.workflow;

import cn.hutool.db.Entity;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dengcj.epl.entity.ecology.workflow.EcologyRequestInfo;
import com.dengcj.epl.entity.ecology.workflow.NodeExecuterResult;
import com.dengcj.epl.enums.ecology.workflow.RequestSrc;
import com.dengcj.util.CjUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dengchunjie
 * @Description: Ecology节点附加操作入口类，处理Ecology传入的数据，并调用指定的类
 * @date 2022/1/13 17:54
 */
public class NodeAction {

    public String execute(String requestStr){
        JSONObject requestJson = new JSONObject(requestStr);
        String classPath = requestJson.getStr("classpath");

        if(CjUtil.isBlank(classPath)){
            return "类名为空";
        }

        try {
            //获得对象的类型
            Class nodeActionClass = Class.forName(classPath);
            EcologyNodeAction ecologyNodeAction = (EcologyNodeAction)nodeActionClass.newInstance();

            int createor = requestJson.getInt("creator");
            int requestid = requestJson.getInt("requestid");
            int workflowid = requestJson.getInt("workflowid");
            int nodeid = requestJson.getInt("nodeid");
            String tablename = requestJson.getStr("tablename");
            String src = requestJson.getStr("src");
            // 请求创建日期（经测试该字段为空）
            String createdate = requestJson.getStr("createdate");
            // 请求创建时间（经测试该字段为空）
            String createtime = requestJson.getStr("createtime");
            // 最后节点ID
            int lastnodeid = requestJson.getInt("lastnodeid");
            // 下一节点ID
            int nextnodeid = requestJson.getInt("nextnodeid");
            // 下一节点类型
            String nextnodetype = requestJson.getStr("nextnodetype");
            // 请求名称
            String requestname = requestJson.getStr("requestname");
            // 当前节点操作者
            int userid = requestJson.getInt("userid");

            Map mainFormMap = JSONUtil.toBean(requestJson.getJSONObject("maindata"), Map.class);
            Map<String, List<Map>> detailFormMap = JSONUtil.toBean(requestJson.getJSONObject("detaildata"), Map.class);

            Entity mainData = Entity.parse(mainFormMap);

            Map<String, List<Entity>> detailData = new HashMap<>();
            for(String key : detailFormMap.keySet()){
                List<Map> dtList = detailFormMap.get(key);
                List<Entity> fieldRowList = new ArrayList<>();
                for (Map map : dtList) {
                    fieldRowList.add(Entity.parse(map));
                }

                detailData.put(key, fieldRowList);
            }

            Map paramsMap = JSONUtil.toBean(requestJson.getJSONObject("actionparams"), Map.class);
            Entity params = Entity.parse(paramsMap);

            EcologyRequestInfo ecologyRequestInfo = new EcologyRequestInfo();
            ecologyRequestInfo.setRequestId(requestid);
            ecologyRequestInfo.setWorkflowId(workflowid);
            ecologyRequestInfo.setTableName(tablename);
            ecologyRequestInfo.setNodeId(nodeid);
            if("submit".equals(src)){
                ecologyRequestInfo.setSrc(RequestSrc.Submit);
            } else if("reject".equals(src)){
                ecologyRequestInfo.setSrc(RequestSrc.Reject);
            }

            ecologyRequestInfo.setCreator(createor);
//            ecologyRequestInfo.setCreatedate(createdate);
//            ecologyRequestInfo.setCreatetime(createtime);
            ecologyRequestInfo.setLastnodeid(lastnodeid);
            ecologyRequestInfo.setNextnodeid(nextnodeid);
            ecologyRequestInfo.setNextnodetype(nextnodetype);
            ecologyRequestInfo.setRequestname(requestname);
            ecologyRequestInfo.setUserid(userid);
            ecologyRequestInfo.setMainData(mainData);
            ecologyRequestInfo.setDetailData(detailData);
            ecologyRequestInfo.setParams(params);
            NodeExecuterResult nodeExecuterResult = ecologyNodeAction.execute(ecologyRequestInfo);
            if(!nodeExecuterResult.getStatus()){
                return nodeExecuterResult.toString();
            }
        } catch (ClassNotFoundException e){
            return "类"+classPath+"不存在";
        } catch (InstantiationException ie){
            return "实例化"+classPath+"对象失败";
        } catch (IllegalAccessException ie){
            return "实例化"+classPath+"对象失败";
        }

        return "";
    }
}
